package com.trolls.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.collections.iterators.IteratorChain;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ViewItemList extends Content{
	protected VerticalPanel vPanelPrincipal;
	protected CompositeList compositeListe;
	protected CompositeShowItem compositeShowItem;
	
	public static int LIST_TEXT = 0;
	public static int  LIST_TABLEAU_IMG = 1;
	//public static int ListCalerie = 2;
	
	public ViewItemList(ArrayList<Item> listItems, final int typeList){
		super("La taverne des trolls");
		HorizontalPanel hPanel = new HorizontalPanel();
		vPanelPrincipal= new VerticalPanel();
		vPanelPrincipal.add(enteteContent);
		
		//FILTRAGE
		HorizontalPanel hPanelFiltre = new HorizontalPanel();
		Label labelFiltre = new Label("Filtrer par : ");
		final ListBox lbFiltre = new ListBox();
		String[] attrs =Troll.getAttributsName();
		for(int i=0;i<attrs.length;i++){
		lbFiltre.addItem(attrs[i], String.valueOf(i));
		}
		lbFiltre.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				LaTerreDesTrolls.get().showItemList(typeList, Integer.parseInt(lbFiltre.getValue(lbFiltre.getSelectedIndex())));
			}
		});
		hPanelFiltre.add(labelFiltre);
		hPanelFiltre.add(lbFiltre);
		vPanelPrincipal.add(hPanelFiltre);

		//CHOIX DU COMPOSITE
		
		if(typeList ==LIST_TEXT){
			CompositeList compositeListe= new CompositeList(listItems);
			hPanel.add(compositeListe);

		}else if(typeList==LIST_TABLEAU_IMG){
			CompositeTabList compositeListe= new CompositeTabList(listItems,3,3);
			hPanel.add(compositeListe);

		}
		

		compositeShowItem= new CompositeShowItem(listItems.get(0));
		hPanel.add(compositeShowItem);

		vPanelPrincipal.setVerticalAlignment(HasAlignment.ALIGN_TOP);

		vPanelPrincipal.add(hPanel);
		
		this.initWidget(vPanelPrincipal);
	}
	public ViewItemList(ArrayList<Item> listItems, int typeList, int triedBy){
		this(trieListItem(listItems,triedBy),typeList);
	}

	public void showItem(Item item){
		compositeShowItem.showItem(item);
	}
	
	private static ArrayList<Item> trieListItem(ArrayList<Item> listItems, int triedBy){
		Item [] list =  listItems.toArray(new Item[listItems.size()]);
		trie(list,triedBy);
		listItems = new ArrayList<Item>(Arrays.asList(list));
		return listItems;
	}
	private static Item [] trie(Item [] list, int numAttribut){
		for (int i=0 ;i<=(list.length-2);i++){
            for (int j=(list.length-1);i < j;j--){
                    if (list[j].getAttributs()[numAttribut].compareToIgnoreCase(list[j-1].getAttributs()[numAttribut]) < 0) //negatif si precede l'argument
                    {
                            Item x=list[j-1];
                            list[j-1]=list[j];
                            list[j]=x;
                    }
            }
		}
		return list;
		
	}
}
